/*
 * Copyright (c) 2008 by 1eEurope (Switzerland) AG, Thalwil
 *
 * Customer:	1eEurope (Switzerland) AG
 * Project:		BKS
 * Created:		30.09.2008, WBL
 *
*/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Xml.Xsl;
using System.IO;
using System.Xml;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;

namespace By1eEurope.BKS.Controls
{
    /// <summary>
    /// Renders the Content of a BKS Image Field to a html image and offers edit controls in edit mode
    /// </summary>
    public class ImageControl : BaseFieldControl
    {
        protected Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector UrlSelector;
        protected Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector LinkUrlSelector;
        protected TextBox AltText;
        protected Label Picture;

        protected override string DefaultTemplateName
        {
            get { return "ImageControlTemplate"; }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                ImageValue fieldValue = new ImageValue();
                if (!string.IsNullOrEmpty(this.UrlSelector.AssetUrl))
                    fieldValue.ImageUrl = this.UrlSelector.AssetUrl.Trim();
                if (!string.IsNullOrEmpty(this.AltText.Text))
                    fieldValue.AltText = this.AltText.Text.Trim();
                if (!string.IsNullOrEmpty(this.LinkUrlSelector.AssetUrl))
                    fieldValue.LinkUrl = this.LinkUrlSelector.AssetUrl.Trim();
                
                string val;
                if (SiteUtil.TryGetValue(fieldValue.ImageUrl, "LongDesc",out val))
                    if (!string.IsNullOrEmpty(val))
                        fieldValue.LongDescQS = "?ImageUrl=" + fieldValue.ImageUrl;

                return fieldValue;
            }
            set
            {
                EnsureChildControls();
                ImageValue fieldValue = (ImageValue)value;
                this.UrlSelector.AssetUrl = fieldValue.ImageUrl;
                this.AltText.Text = fieldValue.AltText;
                this.LinkUrlSelector.AssetUrl = fieldValue.LinkUrl;
            }
        }

        protected override void CreateChildControls()
        {
            if (this.Field == null)
                return;

            base.CreateChildControls();

            if (this.ControlMode == SPControlMode.Display)
                return;

            this.UrlSelector = (Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector)this.TemplateContainer.FindControl("UrlSelector");
            if (this.UrlSelector == null)
                throw new ArgumentException("Corrupted ImageControlTemplate template - missing UrlSelector.");

            this.LinkUrlSelector = (Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector)this.TemplateContainer.FindControl("LinkUrlSelector");
            if (this.LinkUrlSelector == null)
                throw new ArgumentException("Corrupted ImageControlTemplate template - missing LinkUrlSelector.");

            this.AltText = (TextBox)this.TemplateContainer.FindControl("AltText");
            if (this.AltText == null)
                throw new ArgumentException("Corrupted ImageControlTemplate template - missing AltText.");

            this.Picture = (Label)this.TemplateContainer.FindControl("Picture");
            if (this.Picture == null)
                throw new ArgumentException("Corrupted ImageControlTemplate template - missing Picture.");
            this.Picture.Text = this.RenderPicture();
        }

        private string RenderPicture()
        {
            ImageValue value = (ImageValue)this.ListItemFieldValue;
            if (value != null && !string.IsNullOrEmpty(value.ImageUrl))
            {
                string template = this.Field.GetCustomProperty("Template") + "";
                if (template != string.Empty)
                {
                    string output = template.Replace("@SRC", value.ImageUrl);
                    output = output.Replace("@ALT", value.AltText);

                    if (!string.IsNullOrEmpty(value.LongDescQS))
                    {
                        string srvRelSiteCollUrl = SPContext.Current.Web.Site.ServerRelativeUrl;
                        if (srvRelSiteCollUrl == "/")
                            srvRelSiteCollUrl = string.Empty;
                        output = output.Replace("@LONGDESC", "longdesc=\"" + srvRelSiteCollUrl + this.Field.GetCustomProperty("LongDescReader") + value.LongDescQS + "\"");
                    }
                    else
                    {
                        output = output.Replace("@LONGDESC", string.Empty);
                    }

                    output = output.Replace("@LINKURL", value.LinkUrl);
                    return output;
                }
                else if (this.ControlMode == SPControlMode.Edit)
                {
                    return "set the Template property! (&lt;a href=\"@LINKURL\"&gt;&lt;img src=\"@SRC\" alt=\"@ALT\" @LONGDESC&gt;&lt;a&gt;)";
                }
            }
            return null;
        }

        protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output)
        {
        }

        
        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            if (this.ControlMode == SPControlMode.Display)
            {
                string pic = this.RenderPicture();
                output.Write(pic);
            }
            base.Render(output);    // Calls RenderChildren (Edit, New) or RenderFieldForDisplay (Display)
        }

        protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderChildren(writer);
        }
    }
}
